package com.ruoyi.hospital.medicine.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.service.ITPatientMedicineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 患者药品关联Controller
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Controller
@RequestMapping("/hospital/medicine")
public class TPatientMedicineController extends BaseController {
    private String prefix = "hospital/medicine";

    @Autowired
    private ITPatientMedicineService tPatientMedicineService;

    @RequiresPermissions("hospital:medicine:view")
    @GetMapping()
    public String medicine() {
        return prefix + "/medicine";
    }

    @GetMapping("/prescribeMedications")
    public String prescribeMedications(String registeredId, String patientId, ModelMap modelMap) {
        modelMap.addAttribute("registeredId", registeredId);
        modelMap.addAttribute("patientId", patientId);
        return prefix + "/prescribe_medications";
    }


    /**
     * 查询患者药品关联列表
     */
    @RequiresPermissions("hospital:medicine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPatientMedicine tPatientMedicine) {
        startPage();
        List<TPatientMedicine> list = tPatientMedicineService.selectTPatientMedicineList(tPatientMedicine);
        return getDataTable(list);
    }


    @PostMapping("/list/{registeredId}")
    @ResponseBody
    public TableDataInfo queryByregistered(@PathVariable("registeredId") String registeredId) {
        startPage();
        List<TPatientMedicine> list = tPatientMedicineService.queryByregistered(registeredId);
        return getDataTable(list);
    }

    /**
     * 导出患者药品关联列表
     */
    @RequiresPermissions("hospital:medicine:export")
    @Log(title = "患者药品关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TPatientMedicine tPatientMedicine) {
        List<TPatientMedicine> list = tPatientMedicineService.selectTPatientMedicineList(tPatientMedicine);
        ExcelUtil<TPatientMedicine> util = new ExcelUtil<TPatientMedicine>(TPatientMedicine.class);
        return util.exportExcel(list, "患者药品关联数据");
    }

    /**
     * 新增患者药品关联
     */
    @GetMapping("/add")
    public String add(String registeredId, ModelMap modelMap) {
        modelMap.addAttribute("registeredId", registeredId);
        return prefix + "/add";
    }

    /**
     * 新增保存患者药品关联
     */
    @RequiresPermissions("hospital:medicine:add")
    @Log(title = "患者药品关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TPatientMedicine tPatientMedicine) {
        return toAjax(tPatientMedicineService.insertTPatientMedicine(tPatientMedicine));
    }

    /**
     * 修改患者药品关联
     */
    @RequiresPermissions("hospital:medicine:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TPatientMedicine tPatientMedicine = tPatientMedicineService.selectTPatientMedicineById(id);
        mmap.put("tPatientMedicine", tPatientMedicine);
        return prefix + "/edit";
    }

    /**
     * 修改保存患者药品关联
     */
    @RequiresPermissions("hospital:medicine:edit")
    @Log(title = "患者药品关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TPatientMedicine tPatientMedicine) {
        return toAjax(tPatientMedicineService.updateTPatientMedicine(tPatientMedicine));
    }

    /**
     * 删除患者药品关联
     */
    @RequiresPermissions("hospital:medicine:remove")
    @Log(title = "患者药品关联", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tPatientMedicineService.deleteTPatientMedicineByIds(ids));
    }
}
